package com.atguigu.yygh.msm.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.msm.service.MsmService;
import com.atguigu.yygh.msm.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Api(value = "登录")
@RestController
@RequestMapping("/api/sms")
public class MsmApiController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @ApiOperation("发送验证码")
    @GetMapping("send/{phone}")
    public Result sendCode(@PathVariable String phone){
        //从redis获取验证码 如果获取到 返回ok
        System.out.println(phone);
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return Result.ok();
        }
        //获取不到
        code ="123123";
        //生成验证码,整合阿里云的短信服务进行发送
        //boolean isSend = msmService.send(phone,code);
        //放到redis,设置有效期
        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        return Result.ok();
    }
}
